package com.sailfish.springbootdemo.service.db1;

import com.alibaba.fastjson.JSONObject;
import com.sailfish.springbootdemo.configs.UserHeaderHolder;
import com.sailfish.springbootdemo.dao.db1.*;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db1.*;
import com.sailfish.springbootdemo.pojo.db4.Operate;
import com.sailfish.springbootdemo.service.db4.OperateService;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BaseCapacityRuleServiceImpl implements BaseCapacityRuleService {

    @Autowired
    private BaseCapacityRuleDao baseCapacityRuleDao;

    @Autowired
    private LineBaseParamDao lineBaseParamDao;

    @Autowired
    private PcbaLineRelatedDeviceDao pcbaLineRelatedDeviceResourceDao;

    @Autowired
    private PcbaDeviceParamDao pcbaDeviceParamDao;

    @Autowired
    private PcbaDeviceTypeDao pcbaDeviceTypeDao;

    @Autowired
    private LineBaseBigTypeDao lineBaseBigTypeDao;

    @Autowired
    private LineBaseSmallTypeDao lineBaseSmallTypeDao;

    @Autowired
    private OperateService operateService;

    private String operateSmallType = "基线能力项/规则";

    @Override
    public List<BaseCapacityRule> getAllBaseCapacityRule(String searchValue, Integer lineBaseBigTypeId, Integer lineBaseSmallTypeId) {
        List<BaseCapacityRule> list = baseCapacityRuleDao.getAllBaseCapacityRule(searchValue, lineBaseBigTypeId, lineBaseSmallTypeId);
        for (BaseCapacityRule baseCapacityRule : list) {
            LineBaseBigType lineBaseBigTypeById = lineBaseBigTypeDao.getLineBaseBigTypeById(baseCapacityRule.getLineBaseBigTypeId());
            baseCapacityRule.setLineBaseBigType(lineBaseBigTypeById);

            LineBaseSmallType lineBaseSmallTypeById = lineBaseSmallTypeDao.getLineBaseSmallTypeById(baseCapacityRule.getLineBaseSmallTypeId());
            baseCapacityRule.setLineBaseSmallType(lineBaseSmallTypeById);

            /*LineBaseParam lineBaseParamByKey = lineBaseParamDao.getLineBaseParamByKey(baseCapacityRule.getLineBaseParamKey());
            if(lineBaseParamByKey != null){
                baseCapacityRule.setLineBaseParam(lineBaseParamByKey);
            }*/

            String pcbaDeviceParamKeys = baseCapacityRule.getPcbaDeviceParamKeys();
            if (pcbaDeviceParamKeys != null && !"".equals(pcbaDeviceParamKeys)) {
                String[] keyArr = pcbaDeviceParamKeys.split(",");
                List<PcbaDeviceParam> l = new ArrayList<>();
                for (String s : keyArr) {
                    PcbaDeviceParam pcbaDeviceParamByKey = pcbaDeviceParamDao.getPcbaDeviceParamByKey(s);
                    if (pcbaDeviceParamByKey != null) {
                        PcbaDeviceType pcbaDeviceType = pcbaDeviceTypeDao.getById(pcbaDeviceParamByKey.getPcbaDeviceTypeId());
                        pcbaDeviceParamByKey.setPcbaDevice(pcbaDeviceType);
                        l.add(pcbaDeviceParamByKey);
                    }
                }
                baseCapacityRule.setPcbaDeviceParamList(l);
            }

            String pcbaDeviceParamKeys2 = baseCapacityRule.getPcbaDeviceParamKeys2();
            if (pcbaDeviceParamKeys2 != null && !"".equals(pcbaDeviceParamKeys2)) {
                String[] keyArr = pcbaDeviceParamKeys2.split(",");
                List<PcbaDeviceParam> l = new ArrayList<>();
                for (String s : keyArr) {
                    PcbaDeviceParam pcbaDeviceParamByKey = pcbaDeviceParamDao.getPcbaDeviceParamByKey(s);
                    if (pcbaDeviceParamByKey != null) {
                        PcbaDeviceType pcbaDeviceType = pcbaDeviceTypeDao.getById(pcbaDeviceParamByKey.getPcbaDeviceTypeId());
                        pcbaDeviceParamByKey.setPcbaDevice(pcbaDeviceType);
                        l.add(pcbaDeviceParamByKey);
                    }
                }
                baseCapacityRule.setPcbaDeviceParamList2(l);
            }
        }
        return list;
    }

    @Override
    public Result updateBaseCapacityRule(BaseCapacityRule baseCapacityRule) {
        try {
            String userId = UserHeaderHolder.getUserId();

            if (baseCapacityRule.getBaseCapacityRuleId() == null || "".equals(baseCapacityRule.getBaseCapacityRuleId().toString())) {
                return ResultUtil.error(500, "缺少参数baseCapacityRuleId", "Miss Param baseCapacityRuleId", null);
            }

            BaseCapacityRule oldBaseCapacityRule = baseCapacityRuleDao.getBaseCapacityRuleById(baseCapacityRule.getBaseCapacityRuleId());

            baseCapacityRule.setUpdateTime(new Date());
            Integer integer = baseCapacityRuleDao.updateBaseCapacityRule(baseCapacityRule);
            if (integer > 0) {
                // 操作记录
                operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 3, 1, new Date(), JSONObject.toJSONString(oldBaseCapacityRule), JSONObject.toJSONString(baseCapacityRule)));
                return ResultUtil.success(null);
            }
            return ResultUtil.error(500, "操作失败", "Operate Fail", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result addOrUpdateBaseCapacityRule(String mode, BaseCapacityRule baseCapacityRule) {
        try {
            if (baseCapacityRule == null) {
                return ResultUtil.error(500, "缺少参数baseCapacityRule", "Miss Param baseCapacityRule", null);
            }
            if (mode == null || "".equals(mode)) {
                return ResultUtil.error(500, "缺少参数mode", "Miss Param mode", null);
            }
            if (!("edit".equals(mode) || "add".equals(mode))) {
                return ResultUtil.error(500, "mode 参数错误", "mode Param Error", null);
            }

            String userId = UserHeaderHolder.getUserId();

            if ("edit".equals(mode)) {
                if (baseCapacityRule.getBaseCapacityRuleId() == null || "".equals(baseCapacityRule.getBaseCapacityRuleId().toString())) {
                    return ResultUtil.error(500, "缺少参数baseCapacityRuleId", "Miss Param baseCapacityRuleId", null);
                }

                List<BaseCapacityRule> baseCapacityRuleList = baseCapacityRuleDao.judgeUniqueBaseCapacityRule(baseCapacityRule.getLineBaseBigTypeId(), baseCapacityRule.getLineBaseSmallTypeId(), baseCapacityRule.getBaseCapacityRuleId(), baseCapacityRule.getBaseCapacityRuleName());
                if (baseCapacityRuleList.size() > 0) {
                    return ResultUtil.error(500, "修改失败，存在相同名字", "Operate Fail", null);
                }

                BaseCapacityRule oldBaseCapacityRule = baseCapacityRuleDao.getBaseCapacityRuleById(baseCapacityRule.getBaseCapacityRuleId());

                baseCapacityRule.setUpdateTime(new Date());
                Integer integer = baseCapacityRuleDao.updateBaseCapacityRule(baseCapacityRule);
                if (integer > 0) {
                    // 操作记录
                    operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 3, 1, new Date(), JSONObject.toJSONString(oldBaseCapacityRule), JSONObject.toJSONString(baseCapacityRule)));
                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            } else {

                List<BaseCapacityRule> baseCapacityRuleByTypeAndName = baseCapacityRuleDao.getBaseCapacityRuleByTypeAndName(baseCapacityRule.getBaseCapacityRuleName(), baseCapacityRule.getLineBaseBigTypeId(), baseCapacityRule.getLineBaseSmallTypeId());
                if (baseCapacityRuleByTypeAndName.size() > 0) {
                    return ResultUtil.error(500, "该项已存在", "Already Exist", null);
                }

                baseCapacityRule.setCreateTime(new Date());
                Integer integer = baseCapacityRuleDao.addBaseCapacityRule(baseCapacityRule);
                if (integer > 0) {
                    // 操作记录
                    operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 1, 1, new Date(), null, JSONObject.toJSONString(baseCapacityRule)));
                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result deleteBaseCapacityRule(Integer baseCapacityRuleId) {
        try {
            if (baseCapacityRuleId == null || "".equals(baseCapacityRuleId.toString())) {
                return ResultUtil.error(500, "缺少参数baseCapacityRuleId", "Miss Param baseCapacityRuleId", null);
            }
            String userId = UserHeaderHolder.getUserId();
            BaseCapacityRule oldBaseCapacityRule = baseCapacityRuleDao.getBaseCapacityRuleById(baseCapacityRuleId);

            Integer integer = baseCapacityRuleDao.deleteBaseCapacityRuleById(baseCapacityRuleId);
            if (integer > 0) {
                // 操作记录
                operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 2, 1, new Date(), JSONObject.toJSONString(oldBaseCapacityRule), null));
                return ResultUtil.success(null);
            }
            return ResultUtil.error(500, "操作失败", "Operate Fail", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }
}
